package com.tengju.bff.interfaces.shared;

import com.tengju.data.domain.shared.CastUtil;
import com.tengju.user.domain.shared.SensitiveFieldType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/1/26
 */
@Data
@ApiModel(value = "AmountVO", description = "数值对象")
@NoArgsConstructor
public class AmountVO {

    @ApiModelProperty(value = "类型：INDEX:指数 PERCENT:占比")
    private AmountType type;

    @ApiModelProperty(value = "数值")
    private Object amount;

    @ApiModelProperty(value = "展示值")
    private String value;

    @ApiModelProperty(value = "描述")
    private String desc;

    @ApiModelProperty(value = "额外信息")
    private List<Object> extras;

    public AmountVO(AmountType type, Object amount) {
        this.type = type;
        this.amount = amount;
        this.value = type.format(amount);
    }

    public AmountVO(AmountType type, Object amount, String desc) {
        this.type = type;
        this.amount = amount;
        this.value = type.format(amount);
        this.desc = desc;
    }

    public void addExtra(Object obj) {
        if (CollectionUtils.isEmpty(extras)) {
            extras = new ArrayList<>();
        }
        if (obj == null) {
            return;
        }
        extras.add(obj);
    }

    public void mask(SensitiveFieldType sensitiveFieldType) {
        sensitiveFieldType.dealData(value);
        sensitiveFieldType.dealData(CastUtil.castString(amount));
    }


    @Override
    public String toString() {
        return value;
    }
}
